﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQueryDotNetLib.Controls;

namespace jQueryDotNetLib.Extenders
{
    [ToolboxItem(true)]
    [ToolboxData("<{0}:Dialog Selector=\"\" runat=server />")]
    public class Dialog : BaseInteractionExtender
    {
        public Dialog()
        {
            Interaction = "dialog";
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool AutoOpen
        {
            get
            {
                if (ViewState["AutoOpen"] == null) return false;
                return (bool)ViewState["AutoOpen"];
            }
            set
            {
                ViewState["AutoOpen"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Modal
        {
            get
            {
                if (ViewState["Modal"] == null) return false;
                return (bool)ViewState["Modal"];
            }
            set
            {
                ViewState["Modal"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OpenTriggerSelector
        {
            get
            {
                if (ViewState["TriggerSelector"] == null) return "";
                return (string)ViewState["TriggerSelector"];
            }
            set
            {
                ViewState["TriggerSelector"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OpenEvent
        {
            get
            {
                if (ViewState["OpenEvent"] == null) return "click";
                return (string)ViewState["OpenEvent"];
            }
            set
            {
                ViewState["OpenEvent"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CloseTriggerSelector
        {
            get
            {
                if (ViewState["CloseTriggerSelector"] == null) return "";
                return (string)ViewState["CloseTriggerSelector"];
            }
            set
            {
                ViewState["CloseTriggerSelector"] = value;
            }
        }
        string resolvedOpenTriggerSelector = null;
        public string ResolvedOpenTriggerSelector
        {
            get
            {
                if (resolvedOpenTriggerSelector == null)
                    resolvedOpenTriggerSelector = jQueryDotNetLib.Utility.ResolveSelectors(this.OpenTriggerSelector, ((Page.Master != null) ? Page.Master.Controls : Page.Controls));

                return resolvedOpenTriggerSelector;
            }
        }
        string resolvedCloseTriggerSelector = null;
        public string ResolvedCloseTriggerSelector
        {
            get
            {
                if (resolvedCloseTriggerSelector == null)
                    resolvedCloseTriggerSelector = jQueryDotNetLib.Utility.ResolveSelectors(this.OpenTriggerSelector, ((Page.Master != null) ? Page.Master.Controls : Page.Controls));

                return resolvedCloseTriggerSelector;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CloseEvent
        {
            get
            {
                if (ViewState["CloseEvent"] == null) return "click";
                return (string)ViewState["CloseEvent"];
            }
            set
            {
                ViewState["CloseEvent"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool BgiFrame
        {
            get
            {
                if (ViewState["BgiFrame"] == null) return false;
                return (bool)ViewState["BgiFrame"];
            }
            set
            {
                ViewState["BgiFrame"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool CloseOnEscape
        {
            get
            {
                if (ViewState["CloseOnEscape"] == null) return true;
                return (bool)ViewState["CloseOnEscape"];
            }
            set
            {
                ViewState["CloseOnEscape"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Buttons
        {
            get
            {
                if (ViewState["Buttons"] == null) return "";
                return (string)ViewState["Buttons"];
            }
            set
            {
                ViewState["Buttons"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string DialogClass
        {
            get
            {
                if (ViewState["DialogClass"] == null) return "";
                return (string)ViewState["DialogClass"];
            }
            set
            {
                ViewState["DialogClass"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Draggable
        {
            get
            {
                if (ViewState["Draggable"] == null) return true;
                return (bool)ViewState["Draggable"];
            }
            set
            {
                ViewState["Draggable"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Height
        {
            get
            {
                if (ViewState["Height"] == null) return "";
                return (string)ViewState["Height"];
            }
            set
            {
                ViewState["Height"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Hide
        {
            get
            {
                if (ViewState["Hide"] == null) return "";
                return (string)ViewState["Hide"];
            }
            set
            {
                ViewState["Hide"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string MaxHeight
        {
            get
            {
                if (ViewState["MaxHeight"] == null) return "";
                return (string)ViewState["MaxHeight"];
            }
            set
            {
                ViewState["MaxHeight"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string MaxWidth
        {
            get
            {
                if (ViewState["MaxWidth"] == null) return "";
                return (string)ViewState["MaxWidth"];
            }
            set
            {
                ViewState["MaxWidth"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string MinHeight
        {
            get
            {
                if (ViewState["MinHeight"] == null) return "";
                return (string)ViewState["MinHeight"];
            }
            set
            {
                ViewState["MaxHeight"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string MinWidth
        {
            get
            {
                if (ViewState["MinWidth"] == null) return "";
                return (string)ViewState["MinWidth"];
            }
            set
            {
                ViewState["MinWidth"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Position
        {
            get
            {
                if (ViewState["Position"] == null) return "";
                return (string)ViewState["Position"];
            }
            set
            {
                ViewState["Position"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Show
        {
            get
            {
                if (ViewState["Show"] == null) return "";
                return (string)ViewState["Show"];
            }
            set
            {
                ViewState["Show"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Resizable
        {
            get
            {
                if (ViewState["Resizable"] == null) return true;
                return (bool)ViewState["Resizable"];
            }
            set
            {
                ViewState["Resizable"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Stack
        {
            get
            {
                if (ViewState["Stack"] == null) return true;
                return (bool)ViewState["Stack"];
            }
            set
            {
                ViewState["Stack"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Title
        {
            get
            {
                if (ViewState["Title"] == null) return "";
                return (string)ViewState["Title"];
            }
            set
            {
                ViewState["Title"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Width
        {
            get
            {
                if (ViewState["Width"] == null) return 300;
                return (int)ViewState["Width"];
            }
            set
            {
                ViewState["Width"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ZIndex
        {
            get
            {
                if (ViewState["ZIndex"] == null) return 1000;
                return (int)ViewState["ZIndex"];
            }
            set
            {
                ViewState["ZIndex"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool HideTitle
        {
            get
            {
                if (ViewState["HideTitle"] == null) return false;
                return (bool)ViewState["HideTitle"];
            }
            set
            {
                ViewState["HideTitle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool AjaxOnly
        {
            get
            {
                if (ViewState["AjaxOnly"] == null) return false;
                return (bool)ViewState["AjaxOnly"];
            }
            set
            {
                ViewState["AjaxOnly"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientBeforeClose
        {
            get
            {
                if (ViewState["OnClientBeforeClose"] == null) return "";
                return (string)ViewState["OnClientBeforeClose"];
            }
            set
            {
                ViewState["OnClientBeforeClose"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientOpen
        {
            get
            {
                if (ViewState["OnClientOpen"] == null) return "";
                return (string)ViewState["OnClientOpen"];
            }
            set
            {
                ViewState["OnClientOpen"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientFocus
        {
            get
            {
                if (ViewState["OnClientFocus"] == null) return "";
                return (string)ViewState["OnClientFocus"];
            }
            set
            {
                ViewState["OnClientFocus"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientDragStart
        {
            get
            {
                if (ViewState["OnClientDragStart"] == null) return "";
                return (string)ViewState["OnClientDragStart"];
            }
            set
            {
                ViewState["OnClientDragStart"] = value;
            }
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientDrag
        {
            get
            {
                if (ViewState["OnClientDrag"] == null) return "";
                return (string)ViewState["OnClientDrag"];
            }
            set
            {
                ViewState["OnClientDrag"] = value;
            }
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientDragStop
        {
            get
            {
                if (ViewState["OnClientDragStop"] == null) return "";
                return (string)ViewState["OnClientDragStop"];
            }
            set
            {
                ViewState["OnClientDragStop"] = value;
            }
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientResizeStart
        {
            get
            {
                if (ViewState["OnClientResizeStart"] == null) return "";
                return (string)ViewState["OnClientResizeStart"];
            }
            set
            {
                ViewState["OnClientResizeStart"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientResize
        {
            get
            {
                if (ViewState["OnClientResize"] == null) return "";
                return (string)ViewState["OnClientResize"];
            }
            set
            {
                ViewState["OnClientResize"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientResizeStop
        {
            get
            {
                if (ViewState["OnClientResizeStop"] == null) return "";
                return (string)ViewState["OnClientResizeStop"];
            }
            set
            {
                ViewState["OnClientResizeStop"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientClose
        {
            get
            {
                if (ViewState["OnClientClose"] == null) return "";
                return (string)ViewState["OnClientClose"];
            }
            set
            {
                ViewState["OnClientClose"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (OnClientClose != "") base.AddBinding("dialogclose", OnClientClose);
            if (OnClientResizeStop != "") base.AddBinding("resizeStop", OnClientResizeStop);
            if (OnClientResize != "") base.AddBinding("resize", OnClientResize);
            if (OnClientResizeStart != "") base.AddBinding("resizeStart", OnClientResizeStart);
            if (OnClientDragStop != "") base.AddBinding("dragStop", OnClientDragStop);
            if (OnClientDrag != "") base.AddBinding("drag", OnClientDrag);
            if (OnClientDragStart != "") base.AddBinding("dragStart", OnClientDragStart);
            if (OnClientFocus != "") base.AddBinding("dialogfocus", OnClientFocus);
            if (OnClientOpen != "") base.AddBinding("dialogopen", OnClientOpen);
            if (OnClientBeforeClose != "") base.AddBinding("dialogbeforeclose", OnClientBeforeClose);

            if (Width != 300) base.AddParameter("width", Width.ToString());
            if (ZIndex != 1000) base.AddParameter("zIndex", ZIndex.ToString());

            if (Title != "") base.AddParameter("title", "'" + Title + "'");
            if (Show != "") base.AddParameter("show", "'" + Show.ToLower() + "'");

            int val = 0;

            if (Position != "")
                if (int.TryParse(Position, out val))
                    base.AddParameter("position", Position);
                else
                    base.AddParameter("position", "'" + Position + "'");


            if (MinHeight != "")
            {
                if (int.TryParse(MinHeight, out val))
                    base.AddParameter("minHeight", MinHeight);
                else
                    base.AddParameter("minHeight", "'" + MinHeight + "'");
            }
            if (MinWidth != "")
            {
                if (int.TryParse(MinWidth, out val))
                    base.AddParameter("minWidth", MinWidth);
                else
                    base.AddParameter("minWidth", "'" + MinWidth + "'");
            }
            if (MaxHeight != "")
            {
                if (int.TryParse(MaxHeight, out val))
                    base.AddParameter("maxHeight", MaxHeight);
                else
                    base.AddParameter("maxHeight", "'" + MaxHeight + "'");
            }
            if (MaxWidth != "")
            {
                if (int.TryParse(MaxWidth, out val))
                    base.AddParameter("maxWidth", MaxWidth);
                else
                    base.AddParameter("maxWidth", "'" + MaxWidth + "'");
            }
            if (Height != "")
            {
                if (int.TryParse(Height, out val))
                    base.AddParameter("height", Height);
                else
                    base.AddParameter("height", "'" + Height + "'");
            }




            if (Hide != "") base.AddParameter("hide", "'" + Hide + "'");
            if (DialogClass != "") base.AddParameter("dialogClass", "'" + DialogClass + "'");
            if (Buttons != "") base.AddParameter("buttons", "{" + "'" + Buttons + "}");
            if (BgiFrame) base.AddParameter("bgiframe", "true");
            if (!Stack) base.AddParameter("stack", "false");
            if (!Resizable) base.AddParameter("resizable", "false");
            if (!Draggable) base.AddParameter("draggable", "false");
            if (!CloseOnEscape) base.AddParameter("closeOnEscape", "false");
            if (!AutoOpen) base.AddParameter("autoOpen", "false");
            if (Modal) base.AddParameter("modal", "true");

            base.Render(writer);

            writer.Write("<script language=\"javascript\">jQuery(document).ready(function($) {");

            if (HideTitle)
                writer.Write("jQuery('" + ResolvedSelector + "').dialog().parents(\".ui-dialog\").find(\".ui-dialog-titlebar\").remove();"); ;

            if (ResolvedOpenTriggerSelector != null && OpenEvent != "")
            {
                writer.Write("jQuery('" + ResolvedOpenTriggerSelector + "').bind('" + OpenEvent + "',function(){jQuery('#" + ResolvedSelector + "').dialog('open')});");
            }

            if (ResolvedCloseTriggerSelector != null && CloseEvent != "")
            {
                writer.Write("jQuery('" + ResolvedCloseTriggerSelector + "').bind('" + CloseEvent + "',function(){jQuery('#" + ResolvedSelector + "').dialog('close')});");
            }
            if (!AjaxOnly)
            {
               // RecurseFix(ResolvedSelector, writer);
            }
            writer.Write("});</script>");

        }
        private void RecurseFix(System.Web.UI.ControlCollection list, HtmlTextWriter writer)
        {
            foreach (System.Web.UI.Control ctl in list)
            {
                if ((ctl as IPostBackEventHandler) != null)
                {
                    writer.Write("jQuery('#" + ctl.ClientID + "').bind('click', function(){" + Page.ClientScript.GetPostBackEventReference(ctl, null) + "} );");
                }
                if (ctl.Controls != null && ctl.Controls.Count > 0)
                {
                    RecurseFix(ctl.Controls, writer);
                }
            }
        }
    }
}